package com.aaa.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/4/8 9:55
 * @description：所有dao实现类的基础类
 * @modified By：
 * @version: 1。0
 */
public class BaseDao {
    /**
     * 驱动名称
     */
    static String driver = "com.mysql.cj.jdbc.Driver";
    /**
     * 数据库的地址
     */
    static String url = "jdbc:mysql://127.0.0.1:3306/schooldb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
    /**
     * 数据的用户名
     */
    static String username = "root";
    /**
     * 数据库的密码
     */
    static String password = "root";

    /**
     * 加载驱动
     */
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     */
    public Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭连接
     */
    public void closeAll(PreparedStatement ps, ResultSet rs, Connection connection) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != ps) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != connection) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通用的查询方法
     */
    public List<Map<String, Object>> executeQuery(String sql, Object... objects) {
        //准备容器封装数据
        List<Map<String, Object>> rowList = new ArrayList<>();
        Connection connection = this.getConnection();
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
             ps = connection.prepareStatement(sql);
            //设置参数
            if (null != objects && objects.length > 0) {
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i + 1, objects[i]);
                }
            }
            //执行sql
             resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();

            //解析结果集
            while (resultSet.next()){
                //目的是封装一行数据
                Map<String, Object> row = new HashMap();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i + 1);
                    Object columnValue=resultSet.getObject(i+1);
                    row.put(columnName,columnValue);
                }
                rowList.add(row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(ps,resultSet,connection);
        }
        return rowList;
    }


    /**
     * 通用的增删改方法
     */
    public Boolean executeUpdate(String sql, Object... objects) {
        Connection connection = this.getConnection();
        PreparedStatement ps=null;
        try {
             ps = connection.prepareStatement(sql);
            //设置参数
            if (null != objects && objects.length > 0) {
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i + 1, objects[i]);
                }
            }
            //执行sql
            System.out.println("sql:"+sql);
            int i = ps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(ps,null,connection);
        }
        return false;

    }

    public static void main(String[] args) {
        BaseDao baseDao = new BaseDao();
        List<Map<String, Object>> mapList = baseDao.executeQuery("select * from userinfo");
        for (Map<String, Object> stringObjectMap : mapList) {
            System.out.println(stringObjectMap.toString());
        }

    }
}
